package com.ruoyi.system.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.PrizeInfo;
import com.ruoyi.system.mapper.PrizeInfoMapper;
import com.ruoyi.system.mapper.backend.MerchantBackMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PrizeLuckMapper;
import com.ruoyi.system.domain.PrizeLuck;
import com.ruoyi.system.service.IPrizeLuckService;

/**
 * 奖品抽奖Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-07
 */
@Service
public class PrizeLuckServiceImpl extends CxBase implements IPrizeLuckService
{
    @Autowired
    private PrizeLuckMapper prizeLuckMapper;
    @Autowired
    private PrizeInfoMapper prizeInfoMapper;
    @Autowired
    private MerchantBackMapper merchantBackMapper;

    /**
     * 查询奖品抽奖
     * 
     * @param id 奖品抽奖ID
     * @return 奖品抽奖
     */
    @Override
    public PrizeLuck selectPrizeLuckById(Long id)
    {
        PrizeLuck prizeLuck = prizeLuckMapper.selectPrizeLuckById(id);
        PrizeLuck prizeLuck2 = new PrizeLuck();
        prizeLuck2.setSafetyExaminationId(prizeLuck.getSafetyExaminationId());
        List<PrizeLuck> prizeLucks = prizeLuckMapper.selectPrizeLuckList(prizeLuck2);
        float sum = 0;
        for(PrizeLuck prizeLuck1:prizeLucks){
            sum+=prizeLuck1.getPrizeProbability();
        }
        sum = (float)Math.round((1-sum)*1000)/1000;
        prizeLuck.setPrizeSurplusProbability(sum);
        return prizeLuck;
    }

    /**
     * 查询奖品抽奖列表
     * 
     * @param prizeLuck 奖品抽奖
     * @return 奖品抽奖
     */
    @Override
    public List<PrizeLuck> selectPrizeLuckList(PrizeLuck prizeLuck)
    {
        if(!checkNull(prizeLuck.getSafetyExaminationId())){
            List<PrizeLuck> prizeLucks = prizeLuckMapper.selectPrizeLuckList(prizeLuck);
            for(PrizeLuck prizeLuck1:prizeLucks){
                prizeLuck1.setSetDate(StampToString(Long.valueOf(prizeLuck1.getSetDate())));
            }
            return prizeLucks;
        }
        return null;
    }

    /**
     * 新增奖品抽奖
     * 
     * @param prizeLuck 奖品抽奖
     * @return 结果
     */
    @Override
    public int insertPrizeLuck(PrizeLuck prizeLuck)
    {
        if(!checkNull(prizeLuck.getSafetyExaminationId())&&!checkNull(prizeLuck.getPrizeId())&&!checkNull(prizeLuck.getSetId())&&!checkNull(prizeLuck.getPrizeAmount())&&!checkNull(prizeLuck.getPrizeProbability()))
        {
            PrizeLuck prizeLuck2 = new PrizeLuck();
            prizeLuck2.setSafetyExaminationId(prizeLuck.getSafetyExaminationId());
            List<PrizeLuck> prizeLucks = prizeLuckMapper.selectPrizeLuckList(prizeLuck2);
            prizeLuck2.setPrizeId(prizeLuck.getPrizeId());
            if(prizeLuckMapper.selectPrizeLuckList(prizeLuck2).size()<=0 && prizeLuck.getPrizeAmount()>0) {
                PrizeInfo prizeInfo = prizeInfoMapper.selectPrizeInfoById(prizeLuck.getPrizeId());
                if (prizeLuck.getPrizeAmount()>=0&&(prizeLuck.getPrizeAmount() <= (prizeInfo.getPrizeSurplusAmount()))) {
                    float sum = prizeLuck.getPrizeProbability();
                    for(PrizeLuck prizeLuck1:prizeLucks){
                        sum+=prizeLuck1.getPrizeProbability();
                    }
                    if(prizeLuck.getPrizeProbability()<=1) {
                        prizeLuck.setSetDate(getStamp().toString());
                        return prizeLuckMapper.insertPrizeLuck(prizeLuck);
                    }
                }
            }
        }
        return 0;
    }

    /**
     * 修改奖品抽奖
     * 
     * @param prizeLuck 奖品抽奖
     * @return 结果
     */
    @Override
    public int updatePrizeLuck(PrizeLuck prizeLuck)
    {
        if(!checkNull(prizeLuck.getSafetyExaminationId())&&!checkNull(prizeLuck.getPrizeId())&&!checkNull(prizeLuck.getSetId())&&!checkNull(prizeLuck.getPrizeAmount())&&!checkNull(prizeLuck.getPrizeProbability()))
        {
            List<PrizeLuck> prizeLucks = prizeLuckMapper.selectPrizeLuckList(prizeLuck);
            if(prizeLucks.size()>0) {
                PrizeInfo prizeInfo = prizeInfoMapper.selectPrizeInfoById(prizeLuck.getPrizeId());
                if (prizeLuck.getPrizeAmount()>=0&&(prizeLuck.getPrizeAmount() <= (prizeInfo.getPrizeSurplusAmount() + prizeLucks.get(0).getPrizeAmount()))) {
                    prizeLuck.setSetDate(getStamp().toString());
                    return prizeLuckMapper.updatePrizeLuck(prizeLuck);
                }
            }
        }
        return 0;
    }

    /**
     * 批量删除奖品抽奖
     * 
     * @param ids 需要删除的奖品抽奖ID
     * @return 结果
     */
    @Override
    public int deletePrizeLuckByIds(Long[] ids)
    {
        return prizeLuckMapper.deletePrizeLuckByIds(ids);
    }

    /**
     * 删除奖品抽奖信息
     * 
     * @param id 奖品抽奖ID
     * @return 结果
     */
    @Override
    public int deletePrizeLuckById(Long id)
    {
        return prizeLuckMapper.deletePrizeLuckById(id);
    }
}
